#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::ifstream;
using std::istringstream;
using std::runtime_error;

const string& transform(const string &s, const map <string, string> &m) {
    auto map_it = m.find(s);

    if (map_it != m.end()) {
        return map_it->second;
    }else {
        return s;
    }
}

map <string, string> buildMap(ifstream &map_file) {
    map <string, string> trans_map;
    string key;
    string value;
    while (map_file >> key && getline(map_file, value))
        if (value.size() > 1) {
            /* 前面有一个空格 */
            trans_map[key] = value.substr(1);
        }else {
            throw runtime_error("no rule for " + key);
        }
    return trans_map;
}


void word_transform(ifstream& map_file, ifstream& input) {
    auto trans_map = buildMap(map_file);
    string text;

    while (getline(input, text)) {
        istringstream stream(text);
        string word;
        bool firstword = true;

        while (stream >> word) {
            /**
             * 输出一个单词后要输出空格
             */
            if (firstword) {
                firstword = false;
            }else {
                cout << " ";
            }
            cout << transform(word, trans_map);
        }

        cout << endl;
    }
}


int main() {
    ifstream file("../map.txt");
    ifstream map("../file.txt");

    word_transform(file, map);

    return 0;
}
